package cn.jx.asc.common.utils;

import cn.jx.asc.common.config.DataBaseConfig;
import cn.jx.asc.common.config.InjectionConfig;
import cn.jx.asc.common.enums.ResultStatus;
import cn.jx.asc.common.response.Result;
import cn.jx.asc.common.response.ResultUtils;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * 数据库sql脚本的导入导出
 *
 * @author lhg
 */
public class DataBasesManageUtils {


    /**
     * @param fileName 数据脚本文件名
     * @return
     */
    public static Result backup(String fileName) {
        String hostIP = DataBaseConfig.getIp();
        String userName=DataBaseConfig.getUsername();
        String password=DataBaseConfig.getPassword();
        String databaseName=DataBaseConfig.getDataBase();
        fileName = encodingFilename(fileName);
        String savePath = getAbsoluteFile(fileName);

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("mysqldump").append(" --opt").append(" -h").append(hostIP);
        stringBuilder.append(" --user=").append(userName).append(" --password=").append(password)
                .append(" --lock-all-tables=true");
        stringBuilder.append(" --result-file=").append(savePath).append(" --default-character-set=utf8 ")
                .append(databaseName);

        try {

            Process process = Runtime.getRuntime().exec(stringBuilder.toString());
            if (process.waitFor() == 0) {
                String s = "=====SQL脚本导出成功====="+"导出路径为："+savePath+"文件名为："+fileName;
                return ResultUtils.resultWithoutData(ResultStatus.SUCCESS,fileName);
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
        return ResultUtils.resultWithoutData(ResultStatus.FAILED);
    }


    /**
     *
     * @param filepath sql脚本路径
     * @param ip 数据库ip
     * @param database 数据库名
     * @param userName 用户名
     * @param password 密码
     * @return
     */
    public static String recover(String filepath,String ip,String database, String userName,String password) {
        String command1 = "mysqladmin -h "+ip+" -u "+userName+" -p"+password+" create "+database;

        String command2 = "cmd /c mysql -h "+ip+" -u"+userName+" -p"+password+" "+database+" < " + filepath;


        try {
            final Process exec1 = Runtime.getRuntime().exec(command1);
            if (exec1.waitFor() == 0){
            }
            Process exec = Runtime.getRuntime().exec(command2);
            if (exec.waitFor() == 0){
            }
        } catch (IOException e) {
            e.printStackTrace();
            return "数据库恢复失败";
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "数据库恢复成功";
    }
    /**
     * 获取下载路径
     *
     * @param filename 文件名称
     */
    public static String getAbsoluteFile(String filename)
    {
        String downloadPath = InjectionConfig.getDownloadPath() + filename;
        File desc = new File(downloadPath);
        if (!desc.getParentFile().exists())
        {
            desc.getParentFile().mkdirs();
        }
        return downloadPath;
    }

    /**
     * 编码文件名
     */
    public static String encodingFilename(String filename)
    {
        filename = UUID.randomUUID().toString() + "_" + filename + ".sql";
        return filename;
    }
}
